package ar.edu.itba.it.paw.web.user;

import java.util.List;

import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PropertyListView;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import ar.edu.itba.it.paw.domain.Auction;
import ar.edu.itba.it.paw.domain.Category;
import ar.edu.itba.it.paw.domain.Product;
import ar.edu.itba.it.paw.repo.ProductRepo;
import ar.edu.itba.it.paw.repo.UserRepo;
import ar.edu.itba.it.paw.web.MercadoLibre;
import ar.edu.itba.it.paw.web.MercadoLibreSession;
import ar.edu.itba.it.paw.web.base.BasePage;
import ar.edu.itba.it.paw.web.component.TypeLabel;
import ar.edu.itba.it.paw.web.product.AddProductPage;
import ar.edu.itba.it.paw.web.product.BoughtProductsPage;
import ar.edu.itba.it.paw.web.product.EditProductListPage;
import ar.edu.itba.it.paw.web.product.OpenAuctionsPage;
import ar.edu.itba.it.paw.web.product.ProductDetailPage;
import ar.edu.itba.it.paw.web.product.SoldProductsPage;
import ar.edu.itba.it.paw.web.question.AnswerQuestionPage;
import ar.edu.itba.it.paw.web.transaction.CountTransactionCategoryPage;
import ar.edu.itba.it.paw.web.transaction.TransactionCategoryPage;

public class UserMenuPage extends BasePage {

	@SpringBean
	private ProductRepo productRepo;

	
	public UserMenuPage(){
		add(new Link<Void>("countTransactionCategory") {
			@Override
			public void onClick() {
				setResponsePage(CountTransactionCategoryPage.class);
			}
		});
		add(new Link<Void>("transactionCategory") {
			@Override
			public void onClick() {
				setResponsePage(new TransactionCategoryPage(null));
			}
		});
		Link<Void> link = new Link<Void>("lastLogin") {
			@Override
			public void onClick() {
				setResponsePage(LastLoginPage.class);
			}
		};
		if(MercadoLibreSession.get().getUser() != null &&!MercadoLibreSession.get().getUser().isAdmin()){
			link.setVisibilityAllowed(false);
		}
		add(link);
		add(new Link<Void>("listAuctions") {
			@Override
			public void onClick() {
				setResponsePage(OpenAuctionsPage.class);
			}
		});
		add(new Link<Void>("addProducts") {
			@Override
			public void onClick() {
				setResponsePage(AddProductPage.class);
			}
		});
		add(new Link<Void>("editProducts") {
			@Override
			public void onClick() {
				setResponsePage(EditProductListPage.class);
			}
		});
		add(new Link<Void>("boughtProducts") {
			@Override
			public void onClick() {
				setResponsePage(BoughtProductsPage.class);
			}
		});
		add(new Link<Void>("soldProducts") {
			@Override
			public void onClick() {
				setResponsePage(SoldProductsPage.class);
			}
		});
		add(new Link<Void>("answerQuestions") {
			@Override
			public void onClick() {
				setResponsePage(AnswerQuestionPage.class);
			}
		});
		add(new Link<Void>("editUser") {
			@Override
			public void onClick() {
				//TODO: debo instanciar con el usuario logueado??
				setResponsePage(EditUserPage.class);
			}
		});
		add(new Link<Void>("logout") {
			@Override
			public void onClick() {
				MercadoLibreSession.get().invalidate();
				setResponsePage(getApplication().getHomePage());
			}
		});
		IModel<List<Product>> productsModel = new LoadableDetachableModel<List<Product>>() {
			@Override
			protected List<Product> load() {
				return productRepo.getOtherUserProducts(MercadoLibreSession.get().getUser()); 
			}
		};
		
		
		add(new PropertyListView<Product>("otherUserProducts", productsModel) {
			
			@Override
			protected void populateItem(ListItem<Product> item) {
				item.add(new Label("name",new PropertyModel<String>(item.getModel(), "name")).add(new SimpleAttributeModifier("style", "color: #0099FF")));
				item.add(new Label("description").add(new SimpleAttributeModifier("style", "")));
				item.add(new TypeLabel("type", item.getModel()));
				item.add(new Link<Product>("detail", item.getModel()) {
					@Override
					public void onClick() {
						setResponsePage(new ProductDetailPage(getModelObject()));
					}
				}.add(new Image("detail_image", MercadoLibre.DETAIL_ICON).add(new SimpleAttributeModifier("height","15px"))));
			}
		});
		
		
	}
	
}
